package com.efast.dmom.mgmt.service.mes.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.dmom.mgmt.bean.mes.TwBatchDeleteBean;
import com.efast.dmom.mgmt.dao.mes.TwBatchDeleteDao;
import com.efast.dmom.mgmt.service.mes.TwBatchDeleteService;
@Service
public class TwBatchDeleteServiceImpl implements TwBatchDeleteService {

	@Autowired
	private TwBatchDeleteDao twBatchDeleteDao;
	@Override
	public void deleteCheckItem(List<TwBatchDeleteBean> list) {
		if(list!=null && list.size()>0) {
			twBatchDeleteDao.deleteCheckItem(list);
		}
	}
	@Override
	public void deleteDefectCode(List<TwBatchDeleteBean> list) {		
		if(list!=null && list.size()>0) {
			twBatchDeleteDao.deleteDefectCode(list);
		}
	}
	@Override
	public void deleteCheckItemAndDefectCode(List<TwBatchDeleteBean> list) {
		if(list!=null && list.size()>0) {
			twBatchDeleteDao.deleteCheckItemAndDefectCode(list);
		}
	}
	@Override
	public ResultBean deletePlantInfo(List<TwBatchDeleteBean> list) {
		ResultBean result = new ResultBean(true, "'");
		result.setSuccess(true);
		if(list!=null && list.size()>0) {
			for(int i=0;i<list.size();i++) {
				int count = twBatchDeleteDao.selectShopCountByPlantId(list.get(i).getTmBasPlantId());
				if(count>0) {
					result.setSuccess(false);
					result.setMessage("工厂编号:"+list.get(i).getPlantNo()+"被车间引用");
					return result;
				}
			}
			twBatchDeleteDao.deletePlantInfo(list);
		}
		return result;
	}
	@Override
	public ResultBean deleteShopInfo(List<TwBatchDeleteBean> list) {
		ResultBean result = new ResultBean(true, "'");
		result.setSuccess(true);
		if(list!=null && list.size()>0) {
			for(int i=0;i<list.size();i++) {
				int count = twBatchDeleteDao.selectAreaCountByShopId(list.get(i).getTmBasWorkshopId());
				if(count>0) {
					result.setSuccess(false);
					result.setMessage("车间编号:"+list.get(i).getWorkshopNo()+"被区域引用");
					return result;
				}
			}
			twBatchDeleteDao.deleteShopInfo(list);
		}
		return result;
	}
	@Override
	public ResultBean deleteAreaInfo(List<TwBatchDeleteBean> list) {
		ResultBean result = new ResultBean(true, "'");
		result.setSuccess(true);
		if(list!=null && list.size()>0) {
			for(int i=0;i<list.size();i++) {
				int count = twBatchDeleteDao.selectLineCountByAreaId(list.get(i).getTmBasAreaId());
				if(count>0) {
					result.setSuccess(false);
					result.setMessage("区域编号:"+list.get(i).getAreaNo()+"被产线引用");
					return result;
				}
			}
			twBatchDeleteDao.deleteAreaInfo(list);
		}
		return result;
	}
	@Override
	public ResultBean deleteLineInfo(List<TwBatchDeleteBean> list) {
		ResultBean result = new ResultBean(true, "'");
		result.setSuccess(true);
		if(list!=null && list.size()>0) {
			for(int i=0;i<list.size();i++) {
				int count = twBatchDeleteDao.selectStationCountByLineId(list.get(i).getTmBasProductionLineId());
				if(count>0) {
					result.setSuccess(false);
					result.setMessage("产线名称:"+list.get(i).getLineName()+"被工位引用");
					return result;
				}
			}
			twBatchDeleteDao.deleteLineInfo(list);
		}
		return result;
	}
	@Override
	public ResultBean deleteStationInfo(List<TwBatchDeleteBean> list) {
		ResultBean result = new ResultBean(true, "'");
		result.setSuccess(true);
		if(list!=null && list.size()>0) {
			for(int i=0;i<list.size();i++) {
				int count = twBatchDeleteDao.selectCountByStationId(list.get(i).getTmBasStationId());
				if(count>0) {
					result.setSuccess(false);
					result.setMessage("工位编号:"+list.get(i).getStationNo()+"被生产过程引用");
					return result;
				}
			}
			twBatchDeleteDao.deleteStationInfo(list);
		}
		return result;
	}
	@Override
	public ResultBean deleteOperationInfo(Integer operationId) {
		ResultBean result = new ResultBean(true, "'");
		result.setSuccess(true);
		int count = twBatchDeleteDao.selectCountByOperationId(operationId);
		if(count>0) {
			result.setSuccess(false);
			result.setMessage("该工序被工艺路线引用");
			return result;	
		}
		twBatchDeleteDao.deleteOperationInfo(operationId);
		return result;
	}
	@Override
	public boolean addCheckItemAndDefectCode(Map<String,Object> map) {
		boolean flag = true;
		String shopName = (String)map.get("shopName");
		String itemName = (String)map.get("itemName");
		String defectCode = (String)map.get("defectCode");
		List<TwBatchDeleteBean> selectCheckItemAndDefectCode = twBatchDeleteDao.selectCheckItemAndDefectCode();
		for (TwBatchDeleteBean bean : selectCheckItemAndDefectCode) {
			if (bean.getShopName() != null && bean.getDefectCode() != null && bean.getItemName() != null) {
				if (bean.getShopName().equals(shopName) && bean.getItemName().equals(itemName) && bean.getDefectCode().equals(defectCode)) {
					flag = false;
					break;
				}
			}
		}
		return flag;
	}

}
